package com.marketingexpert.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.marketingexpert.config.Configuration;
import com.marketingexpert.util.Resources;
import com.marketingexpert.util.WebUtils;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

	private static Logger logger = Logger.getLogger(SecurityInterceptor.class);

	private Configuration configuration;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		if (request.getServletPath().startsWith("/unsec")) {
			return true;
		}

		if (request.getSession().getAttribute("parentUserId") != null
				|| request.getSession().getAttribute("taobaoSessionKey") != null
				|| request.getParameter("top_session") != null) {
			return true;
		} else {	
			request.getSession().setAttribute("sessionExpired", Resources.getString("SystemSessionExpired"));
			response.sendRedirect(WebUtils.getBasepath(request) + "/pages/sessionExpiredTip.jsp");
			return false;
		}

	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

}